/*自身对应的.h文件*/
#include "ui_sensordata.h"

/*程序内部对应的.h文件*/
#include "log.h"
#include "app_sensor_data.h"

/*外部库对应的.h文件*/
#include "ui.h"

struct
{
	lv_obj_t** data_member;
	size_t data_member_count;
}g_this_page_data = {
	.data_member = NULL,
	.data_member_count = 0,
};


/***************************************************************
 * Name:	 ui_sensordata_init()
 * Input : void
 * Output: void
 * Return: 0成功 <0失败
 * Author: hwl
 * Revise: V1.0
 * Description: 传感器界面初始化
 ***************************************************************/
int ui_sensordata_init(void)
{	
	sensor_data_t* list_head = NULL;
	size_t list_count = 0;
	app_sensor_data_get_data(	NULL, 0, &list_head, &list_count);
	if(list_count != 0)
	{
		g_this_page_data.data_member = malloc(sizeof(lv_obj_t*) * list_count);
		g_this_page_data.data_member_count = list_count;

		for(int i = 0; i < list_count; i++)
		{
			g_this_page_data.data_member[i] = ui_SensorDataMem_create(ui_SensorDataPanelData);
			lv_obj_set_x(g_this_page_data.data_member[i], -2);
			lv_obj_set_y(g_this_page_data.data_member[i], -2);

			/*设置显示内容*/
			sensor_data_t *sensor = sensor_data_list_col(list_head, i);
			/*编号*/
			lv_obj_t *lv_num = ui_comp_get_child(g_this_page_data.data_member[i], UI_COMP_SENSORDATAMEM_SENSORDATAMEMLABNUM);
			lv_label_set_text_fmt(lv_num, "%03d", i+1);
			/*日期*/
			lv_obj_t *lv_date = ui_comp_get_child(g_this_page_data.data_member[i], UI_COMP_SENSORDATAMEM_SENSORDATAMEMLABDATE);
			char str_date[20] = "";
			sensor_data_get_date(sensor, str_date);
			lv_label_set_text_fmt(lv_date, "%s", str_date);
			/*SN*/
			lv_obj_t *lv_sn = ui_comp_get_child(g_this_page_data.data_member[i], UI_COMP_SENSORDATAMEM_SENSORDATAMEMLABSN);
			char str_sn[11] = "";
			sensor_data_get_sn(sensor, str_sn);
			lv_label_set_text_fmt(lv_sn, "%s", str_sn);
			/*TYPE*/
			lv_obj_t *lv_type = ui_comp_get_child(g_this_page_data.data_member[i], UI_COMP_SENSORDATAMEM_SENSORDATAMEMLABTYPE);
			sensor_type_t type;
			sensor_data_get_type(sensor, &type);
			char str_type[10] = "";
			sensor_data_type_to_str(type, str_type);
			lv_label_set_text_fmt(lv_type, "%s", str_type);
			/*DATA*/
			lv_obj_t *lv_data = ui_comp_get_child(g_this_page_data.data_member[i], UI_COMP_SENSORDATAMEM_SENSORDATAMEMLABDATA);
			float data;
			sensor_data_get_data(sensor, &data);
			lv_label_set_text_fmt(lv_data, "%.2f", data);
		}
		/*释放获取到的信息*/
		app_sensor_data_free_get_data(list_head, list_count);
	}
	
	return 0;
}

/***************************************************************
 * Name:	 ui_sensordata_release()
 * Input : void
 * Output: void
 * Return: 0成功 <0失败
 * Author: hwl
 * Revise: V1.0
 * Description: 传感器界面释放
 ***************************************************************/
int ui_sensordata_release(void)
{
	/*删除生成的图标*/
	for(int i = 0; i < g_this_page_data.data_member_count; i++)
	{
		lv_obj_del(g_this_page_data.data_member[i]);
	}
	free(g_this_page_data.data_member);

	g_this_page_data.data_member_count = 0;

	return 0;
}

/***************************************************************
 * Name:	 ui_sensordata_update()
 * Input : void
 * Output: void
 * Return: void
 * Author: hwl
 * Revise: V1.0
 * Description: 传感器界面更新
 ***************************************************************/
void ui_sensordata_update(void)
{

}

/***************************************************************
 * Name:	 btn_back_cb()
 * Input : param事件参数
 * Output: void
 * Return: 0成功 <0失败
 * Author: hwl
 * Revise: V1.0
 * Description: 返回按键按下回调
 ***************************************************************/
static int btn_back_cb(ui_event_param_t param)
{
	ui_sensordata_release();
	lv_scr_load(ui_Main);

	return 0;
}

/***************************************************************
 * Name:	 btn_select_cb()
 * Input : param事件参数
 * Output: void
 * Return: 0成功 <0失败
 * Author: hwl
 * Revise: V1.0
 * Description: 选择按键按下回调
 ***************************************************************/
static int btn_select_cb(ui_event_param_t param)
{
	LOG_DEBUG("btn_select_cb\r\n");
	return 0;
}

/***************************************************************
 * Name:	 btn_save_cb()
 * Input : param事件参数
 * Output: void
 * Return: 0成功 <0失败
 * Author: hwl
 * Revise: V1.0
 * Description: 返回按键按下回调
 ***************************************************************/
static int btn_save_cb(ui_event_param_t param)
{
	LOG_DEBUG("btn_save_cb\r\n");

	return 0;
}

/*UI事件回调列表*/
static ui_event_cb_list_t g_event_cb_list[] = {
	{
		.obj_name = SENSORDATA_BTN_BACK_CLICK_CB,
		.obj_cb = btn_back_cb,
	},
	{
		.obj_name = SENSORDATA_BTN_SELECT_CLICK_CB,
		.obj_cb = btn_select_cb,
	},
	{
		.obj_name = SENSORDATA_BTN_SAVE_CLICK_CB,
		.obj_cb = btn_save_cb,
	},
};

/***************************************************************
 * Name:	 ui_sensordata_event_cb()
 * Input : param 事件参数
 * Output: void
 * Return: 0成功 <0失败
 * Author: hwl
 * Revise: V1.0
 * Description: 传感器数据界面事件回调函数
 ***************************************************************/
int ui_sensordata_event_cb(ui_event_param_t param)
{
	for(uint8_t i = 0; i < sizeof(g_event_cb_list) / sizeof(ui_event_cb_list_t); i++)
	{
		if(strcmp(g_event_cb_list[i].obj_name, param.obj_name) == 0)
		{
			return g_event_cb_list[i].obj_cb(param);
		}
	}

	return -1;
}